/* eslint-disable no-throw-literal */
let list = {
   customAttribute(el, name, val) {
      if (val) {
         return el.setAttribute(`data-${name}`, val)
      } else {
         return el.getAttribute(`data-${name}`)
      }
   },
   removeAttribute(elem, name) {
      if (elem.getAttribute(name)) {
         return elem.removeAttribute(name)
      }
   },
   addClass(el, className) {
      if (this.hasClass(el, className)) {
         return
      }
      let newClass = el.className.split(' ')
      newClass.push(className)
      el.className = newClass.join(' ')
   },
   hasClass(el, className) {
      let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
      return reg.test(el.className)
   },
   removeClass(el, className) {
      if (this.hasClass(el, className)) {
         var reg = new RegExp('(\\s|^)' + className + '(\\s|$)')
         el.className = el.className.replace(reg, '')
      }
   },
   JudgeAnObject({obj}) { // 检测一个对象是否为空
      return Object.keys(obj).length !== 0
   },
   VerifyData({obj}) {
      console.log(obj)
      return (obj.data.data.code === 1) && (obj.data.data.status === 200)
   }
}

let validate = (function () {
   var types = {
      isUserName(value) {
         let reg = /^([A-Za-z0-9]{4,8}$)|^([\u4E00-\u9FA5A-Za-z0-9]{3,8}$)/

         if (value === '') {
            return '用户名不能为空'
         }
         if (value.length > 8 || value.length < 3) {
            return '用户名只能在3至8位字符,汉字和字母数字组成'
         }
         if (reg.test(value)) {
            return reg.test(value)
         } else {
            return '用户名含有特殊符号，只能使用汉字和字母数字组成3至8'
         }
         return false
      },
      isPassword: function (value) {
         var reg = /^(([A-Za-z0-9])|([\\._])){6,16}$/
         if (value === '') {
            return '密码不能为空'
         }
         if (value.length > 16 || value.length < 6) {
            return '密码只能在6-16位长度'
         }
         if (reg.test(value)) {
            return true
         } else {
            return "密码含有特殊符号，请重新输入密码"
         }
      },
      isEmail(value) {
         let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
         if (value === '') {
            return '邮箱不能为空'
         }
         if (reg.test(value)) {
            return true
         } else {
            return '邮箱格式不正确'
         }
         return false
      }
   }
   return function (value, type) { // type为检测类型,value为检测的值
      if (!types[type]) {
         throw '检测类型不存在'
      }
      if (types[type](value)) {
         return types[type](value)
      }
      return false
   }
})()

function setCookie(cname, cvalue, exdays) {
   var d = new Date();
   d.setTime(d.getTime() + (exdays*24*60*60*1000));
   var expires = "expires="+d.toUTCString();
   document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
   var name = cname + "=";
   var ca = document.cookie.split(';');
   for(var i=0; i<ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1);
      if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
   }
   return "";
}

function clearCookie(name) {
   setCookie(name, "", -1);
}


export const clearCookies = clearCookie
export const lists = list
export const validates = validate
